package com.it.tydic.estate.common.wrapper;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;

import java.io.Serializable;

@Data
public class Wrapper<T> implements Serializable {

    private static final long serialVersionUID = 4646314053305965942L;

    /**
     * 成功码
     */
    public static final int SUCCESS_CODE = 200;

    /**
     * 成功信息
     */
    public static final String SUCCESS_MESSAGE = "操作成功";

    /**
     * 错误码
     */
    public static final int ERROR_CODE = 500;

    /**
     * 错误信息
     */
    public static final String ERROR_MESSAGE = "内部异常";

    /**
     * 错误码：非法参数
     */
    public static final int ILLEGAL_ARGUMENT_CODE = 100;

    /**
     * 错误信息：非法参数
     */
    public static final String ILLEGAL_ARGUMENT_MESSAGE = "非法参数";

    //编号
    private int code;
    //信息
    private String message;
    //结果数据
    private T result;

    Wrapper(int code , String message , T obj) {
        this.code(code).message(message).result(obj);

    }
    Wrapper (int code , String message ) {
        this.code = code;
        this.message = message;
        this.result = null;
    }
    Wrapper () {
        this(SUCCESS_CODE,SUCCESS_MESSAGE);
    }


    private Wrapper<T> code(int code) {
        this.setCode(code);
        return this;
    }
    private Wrapper<T> message(String message) {
        this.setMessage(message);
        return this;
    }
    private Wrapper<T> result (T result) {
        this.setResult(result);
        return this;
    }

    /**
     * 判断是否成功
     * @return
     */
    @JSONField(serialize = false)
    public boolean success() {
        return Wrapper.SUCCESS_CODE == this.code;
    }
}
